/*
 * @Author: 姚潘涛
 * @Date: 2024-06-03 10:40:17
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-10-10 17:25:59
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#ifndef BRAINSYS_DRIVERS_UTILS_WATCH_DOG_H
#define BRAINSYS_DRIVERS_UTILS_WATCH_DOG_H

#include <chrono>
#include <cstdlib>
#include <ctime>
#include <memory>
#include <mutex>

namespace xict_common {
    namespace utils {
        /**
         * @brief 看门狗
         *
         */
        class WatchDog {
        public:
            typedef std::shared_ptr<WatchDog> Ptr;

            /**
             * @brief Construct a new Watch Dog object
             *
             */
            WatchDog();

            /**
             * @brief Construct a new Watch Dog object
             *
             * @param _max_wait_second
             */
            WatchDog(double _max_wait_second);

            /**
             * @brief 喂狗
             *
             */
            void Feed();

            /**
             * @brief 检测是否OK
             *
             * @return true
             * @return false
             */
            bool OK();

            /**
             * @brief 设置最大等待时间
             *
             * @param _sec
             */
            void SetMaxWaitTime(double _sec);

        private:
            /// @brief 时间
            std::chrono::time_point<std::chrono::system_clock> start_, end_;
            /// @brief 开启标志位
            bool start_flag_;
            /// @brief 最大等待时间
            double max_wait_time_sec_;
            /// @brief 锁
            std::mutex mtx_;
        };
    }   // namespace utils
}   // namespace xict_common
#endif